package com.youku.component.notification;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.youku.component.report.ReportHolder;
import com.youku.util.Consts;
import com.youku.util.PropertyReader;

/**
 * 通知的控制器,存储多个project，为每个project执行通知控制
 * 
 * @author loda
 *
 */
public class NotificationController {

	private static final List<Notification> notifications;

	private static Map<String, List<Recipient>> recipientsMap;

	private static String location;
	
	private static final Logger logger = LoggerFactory.getLogger(NotificationController.class);

	static {
		notifications = new LinkedList<>();
		recipientsMap = new HashMap<>();
		notifications.add(new MessageNotification());
		notifications.add(new EMailNotification());
		location=PropertyReader.read(Consts.MAIL_LOCATION);
	}

	/**
	 * 添加通知信息
	 * 
	 * @param projectId
	 *            项目id
	 * @param display
	 *            展现样式
	 * @param recipients
	 *            接收人
	 */
	public static void addNotificationConfig(String projectId, String display, List<Recipient> recipients) {

		logger.info("测试项目{}的展现形式为{}，接收人列表为：\n{}", projectId, display, recipients.toString());

		// 绑定项目id和报告
		ReportHolder.bindReportDisplayWithProject(projectId, display);

		recipientsMap.put(projectId, recipients);

	}

	/**
	 * 给制定项目发送测试报告
	 * 
	 * @param projectId
	 */
	public static void doNotify(String projectId) {
		for (Notification n : notifications) {
			n.notice(ReportHolder.getReport(projectId), recipientsMap.get(projectId));
		}
	}

	public static String getLocation() {
		return location;
	}
}
